// 导入 axios
import axios from 'axios';

// 创建一个 axios 实例，用于发送请求
const catalog_axios = axios.create({
    // 设置后端 API 的基础 URL
    baseURL: 'http://localhost:8080/categories',
    mode: "cors",
    // withCredentials: true,
    // 设置请求头
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
});

// 定义一个用于获取数据的函数
export default {
    promise_process (promise){
        return promise.then( data => [null, data.data])
                      .catch( err => [err, null]);
    },

    async getCategories() {
        return await catalog_axios.get("");
    },

    async getProducts(categoryID){
        return await catalog_axios.get(`/${categoryID}/products`);
    },

    async getAllProducts(){
        const response = await axios({
            url             : `http://localhost:8080/products`,
            mode            : 'cors',
            method          : 'GET',     
        })
        return response
    },

    async getProductById(productID){
        return await catalog_axios.get(`/category/products/${productID}`);
    },

    async getProductByName(product){

        return await catalog_axios.get(`/category/products/${product}`);
    },

    async getItems(categoryID,productID){
        return await catalog_axios.get(`/${categoryID}/products/${productID}/items`)
    },

    async getItem(itemID){
        return await catalog_axios.get(`/category/products/product/items/${itemID}`)
    }
};
